package com.ztstech.matisse_android_plugin.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;

import android.util.DisplayMetrics;
import android.util.Log;

import androidx.annotation.Nullable;


import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 所有activity基类
 * @author zhiyuan
 * @date 2017/7/27
 */

public abstract class BaseActivity extends Activity {

    private Unbinder unbinder;
//    protected ImmersionBar mImmersionBar;
    private boolean fullScreen;
    private boolean noTitleBar;
    private boolean tophasColor;
    private boolean splashFull;
    private boolean mSystemFontDark = true;

    /** 是否在后台 */
    protected boolean isCurrentRunningForeground = true;

    @Override
    protected final void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onSuperCreateFinish(savedInstanceState);
        setContentView(getLayoutRes());
        initFontScale();
        unbinder = ButterKnife.bind(this);
//        mImmersionBar = ImmersionBar.with(this).reset().navigationBarEnable(false);
//        mImmersionBar.statusBarColor(R.color.color_109).statusBarAlpha(0f).flymeOSStatusBarFontColor(R.color.list_split_item_color);
        /**
         * 以上两行代码必须放在onViewBindFinish前 因为需要onViewBindFinish对不同的页面进行微调整（需要实例化mImmersionBar这个对象）
         */
        Log.e("currentView",getLocalClassName());
//        if (BuildConfig.DEBUG){
            onViewBindFinish(savedInstanceState);
            onViewBindFinish();
//        } else {
//            try {
//                onViewBindFinish(savedInstanceState);
//                onViewBindFinish();
//            }catch (Exception e){
////                MobclickAgent.reportError(this,getLocalClassName() +
////                        "发生异常：" + e.getMessage());
//            }
//        }

//        Properties prop= new Properties();
//        try {
//            prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        setSystemBar();
    }

//    protected void setSystemBar(){
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ||
//                TextUtils.equals(CommonUtil.getPhoneModel(),"OPPO R7sm")){
//            // 只对5.0以上进行状态栏适配
//            return;
//        }
//        if(splashFull){
//            mImmersionBar.reset().
//                    navigationBarEnable(false).
//                    fullScreen(true).
//                    flymeOSStatusBarFontColor(R.color.list_split_item_color).
//                    statusBarDarkFont(mSystemFontDark).
//                    init();
//            return;
//        }
//        if (!ImmersionBar.isSupportStatusBarDarkFont()) {
//            mImmersionBar.statusBarDarkFont(true, 0.2f);
//        }
//        if(noTitleBar){
//            mImmersionBar.reset().
//                    navigationBarEnable(false).
//                    fullScreen(true).
//                    statusBarColor(R.color.color_109).
//                    statusBarAlpha(0.5f).
//                    statusBarDarkFont(mSystemFontDark).
//                    flymeOSStatusBarFontColor(R.color.list_split_item_color).
//                    init();
//        }else if(!fullScreen){
//            mImmersionBar.fitsSystemWindows(true).keyboardEnable(true);
//            if(!tophasColor){
//                mImmersionBar.
//                        statusBarColor(R.color.color_109).
//                        statusBarAlpha(0.5f).
//                        statusBarDarkFont(mSystemFontDark).
//                        flymeOSStatusBarFontColor(R.color.list_split_item_color).
//                        init();
//            }else {
//                mImmersionBar.init();
//            }
//        }
//
//    }

    /**
     * 如果没有ActionBar 调用此方法参数传true
     * @param noTitleBar
     */
    protected void noTitleBar(boolean noTitleBar){
        this.noTitleBar = noTitleBar;
    }

    /**
     * 设置状态栏颜色是否为深色
     * @param mSystemFontDark
     */
    protected void setSystemFontDark(boolean mSystemFontDark) {
        this.mSystemFontDark = mSystemFontDark;
    }

    /**
     * 需要隐藏状态栏实现全屏的调用此方法 参数为true
     * @param splashFull
     */
    protected void splashFull(boolean splashFull){
        this.splashFull = splashFull;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mImmersionBar.destroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /**
     * 设置沉浸式
     * @param barColor 状态栏颜色
     * @param fontColor 状态栏字体颜色
     */
    protected void setTopBarColorAndTextColor(int barColor,int fontColor){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            // 只对6.0以上进行状态栏适配
            return;
        }
        tophasColor = true;
//        mImmersionBar.statusBarColor(barColor).flymeOSStatusBarFontColor(fontColor);
    }

    /**
     * 设置浅色背景色及黑色字体
     */
    protected void setTopBarLightColorAndDarkTextColor(int backColor){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            // 只对6.0以上进行状态栏适配
            return;
        }
        tophasColor = true;
//        mImmersionBar.statusBarColor(backColor).statusBarDarkFont(true);
    }

    /**
     * 设置深色背景色及白色字体
     */
    protected void setTopBarDarkColorAndLightTextColor(int backColor){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            // 只对6.0以上进行状态栏适配
            return;
        }
        tophasColor = true;
//        mImmersionBar.statusBarColor(backColor).statusBarDarkFont(false);
    }

    /**
     * 设置是否为全屏
     */
    protected void setFullScreen(boolean fullScreen){
        this.fullScreen = fullScreen;
    }

    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
    }

    protected void onSuperCreateFinish(@Nullable Bundle savedInstanceState) {}

    protected void onViewBindFinish(@Nullable Bundle savedInstanceState) {}

    protected void onViewBindFinish() {}

    protected abstract int getLayoutRes();

    @Override
    protected void onStart() {
        super.onStart();
//        MobclickAgent.onPageStart(getUmengPageTitle());
    }

    @Override
    protected void onStop() {
        super.onStop();
//        MobclickAgent.onPageEnd(getUmengPageTitle());
        isCurrentRunningForeground = isRunningForeground();
    }

    protected String getUmengPageTitle() {
        return this.getClass().getSimpleName();
    }

    public boolean isRunningForeground() {
        ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        if (appProcessInfos == null){
            return false;
        }
        // 枚举进程
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName.equals(this.getApplicationInfo().processName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 有些手机调大字体默认再改为标准字体
     */
    private void initFontScale() {
        Configuration configuration = getResources().getConfiguration();
        configuration.fontScale = (float) 1;
        //0.85 小, 1 标准大小, 1.15 大，1.3 超大 ，1.45 特大
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);
    }

}
